package com.liangchao.cameratwoapi2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "liangchao";

    private static final String SEPARATOR = File.separator;//路径分隔符

    Context appContext;

    WebView webView;
    Button bt1;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.web1);
        bt1 = (Button) findViewById(R.id.bt1);
        appContext = getApplicationContext();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                File htmlfile = new File(getExternalFilesDir(null).getAbsolutePath()+"/video.html");
                if (!htmlfile.exists()){
                    copyFilesFromRaw(appContext,R.raw.videoht,"video.html",getExternalFilesDir(null).getAbsolutePath());
                }

                File mp4file = new File(getExternalFilesDir(null).getAbsolutePath()+"/Video.mp4");
                if (!mp4file.exists()){
                    copyFilesFromRaw(appContext,R.raw.test,"Video.mp4",getExternalFilesDir(null).getAbsolutePath());
                }

            }
        });
        thread.start();
        thread.run();
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WebSettings webSettings = webView.getSettings();
                webSettings.setMediaPlaybackRequiresUserGesture(false);
                webSettings.setJavaScriptEnabled(true);
                webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
                webSettings.setAllowUniversalAccessFromFileURLs(true);
                webSettings.setAllowFileAccessFromFileURLs(true);
                webSettings.setAllowFileAccess(true);
                webSettings.setAllowContentAccess(true);
                File htmlfile = new File(getExternalFilesDir(null).getAbsolutePath()+"/video.html");
                if(htmlfile.exists())
                {
                    if(htmlfile.isFile()) {
                        Log.e(TAG, "html file path: " + htmlfile.getAbsolutePath());
                        webView.loadUrl("file://" + htmlfile.getAbsolutePath());
                    }
                }
            }
        });

    }


    public static void copyFilesFromRaw(Context context, int id, String fileName, String storagePath) {
        InputStream inputStream = context.getResources().openRawResource(id);
        File file = new File(storagePath);
        //如果文件夹不存在，则创建新的文件夹
        if (!file.exists()) {
            file.mkdirs();
        }
        readInputStream(storagePath + SEPARATOR + fileName, inputStream);
    }

    /**
     * 读取输入流中的数据写入输出流
     *
     * @param storagePath 目标文件路径
     * @param inputStream 输入流
     */
    public static void readInputStream(String storagePath, InputStream inputStream) {
        File file = new File(storagePath);
        try {
            if (!file.exists()) {
                // 1.建立通道对象
                FileOutputStream fos = new FileOutputStream(file);
                // 2.定义存储空间
                byte[] buffer = new byte[inputStream.available()];
                // 3.开始读文件
                int lenght = 0;
                while ((lenght = inputStream.read(buffer)) != -1) {// 循环从输入流读取buffer字节
                    // 将Buffer中的数据写到outputStream对象中
                    fos.write(buffer, 0, lenght);
                }
                // 刷新缓冲区
                fos.flush();
                // 4.关闭流
                fos.close();
                inputStream.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}